package com.lanswon.qzsmk.controller.cardManageController;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.service.cardManageService.ApplyCardService;
import com.lanswon.qzsmk.service.cardManageService.MakeCardService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.vo.MakeCardQueryConditionVo;
import com.lanswon.qzsmk.vo.MakeCardQueryResultVo;
import com.lanswon.qzsmk.vo.PersonalVo;

/**
 * 批量制卡Controller
 * @author guyawei
 *
 */
@Controller
@RequestMapping(value="/makeCardBatch")
public class MakeCardBatchController {
	
	@Autowired
	ApplyCardService acs;
	
	@Autowired
	ParamManageService pm;
	
	@Autowired
	MakeCardService mcs;
	
	/**
	 * 跳转批量制卡查询页面
	 * @return
	 */
	@RequiresPermissions("makeCardBatch:index")
	@RequestMapping(value="/batch",name="批量制卡,makeCardBatch:index",method=RequestMethod.GET)
	public String makeCardBatch(){
		return "MakeCardBatch";
	}
	
	/**
	 * 根据申请批次号获得个人信息，跳转任务生成页面中dialog
	 * @return
	 */
	@RequiresPermissions("makeCardBatch:infosByTasking")
	@RequestMapping(value="/infosByTasking",name="批量制卡个人信息dialog页面,makeCardBatch:infosByTasking",method=RequestMethod.GET)
	public String ApplicationInfos(String parm,HttpServletRequest request){
		MakeCardQueryConditionVo mcv=new MakeCardQueryConditionVo();
		mcv.setApplyNo(parm);
		mcv.setCurrentPage(1);
		mcv.setNumPerPage(20);
		mcv.setStartLine((mcv.getCurrentPage()-1)*mcv.getNumPerPage());
		DwzPage<PersonalVo> dPage=mcs.queryPersonalByApplybatchNo(mcv);
		request.getSession().setAttribute("dPage", dPage);
		return "ApplicationInfos";
	}
	
	/**
	 * 获取要生成制卡任务的信息
	 * @param 
	 * @return
	 * 
	 */
	@RequiresPermissions("makeCardBatch:getMakeCardInfo")
	@RequestMapping(value="/getMakeCardInfo",name="批量制卡获取要生成制卡任务的信息,makeCardBatch:getMakeCardInfo",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<MakeCardQueryResultVo> getMakeCardInfo(MakeCardQueryConditionVo mcv,Integer pageNum,Integer numPerPage)  {
		System.out.println(mcv);
		DwzPage<MakeCardQueryResultVo> dPage=null;
//		if(mcv.getNumPerPage()==0){//页面加载执行
//			mcv.setCurrentPage(1);
//			mcv.setNumPerPage(20);
//			
//		}else{//页面分页
//			if(mcv.getCurrentPage()<=1||pageCount==0){
//				mcv.setCurrentPage(1);
//			}else if(mcv.getCurrentPage()>=pageCount&&pageCount!=0){
//				mcv.setCurrentPage(pageCount);
//			}
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;
			
		}
		mcv.setCurrentPage(pageNum);
		mcv.setNumPerPage(numPerPage);
		mcv.setStartLine((mcv.getCurrentPage()-1)*mcv.getNumPerPage());
		dPage= mcs.queryBatchCollection(mcv);
		System.out.println(dPage.getPagedList().size()+"条数据*********");
		return dPage;
	}
	/**
	 * 生成制卡任务
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("makeCardBatch:createTask")
	@RequestMapping(value="/createTask",name="批量制卡生成制卡任务,makeCardBatch:createTask",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> goExportTask(String bankCode,String companyCode,String[]array) throws Exception{
		System.out.println("制卡任务%"+companyCode+"%"+bankCode+"%"+"%"+array.length);
		List<MakeCardQueryResultVo> list=new ArrayList<MakeCardQueryResultVo>();
			for(int i=0;i<array.length;i++){
				MakeCardQueryResultVo mcrvo=new MakeCardQueryResultVo();
				String item=array[i];
				mcrvo.setProperties(item.split("#"));
				if(companyCode==""||companyCode==null){
					System.out.println("设置银行");
					mcrvo.setCompanyNo(bankCode);
				}else if(bankCode==""||bankCode==null){
					System.out.println("设置制卡商");
					mcrvo.setCompanyNo(companyCode);
				}else{
					System.out.println("设置ALL");
					this.setcompanyNo(mcrvo, bankCode, companyCode);
				}
				System.out.println(mcrvo);
				list.add(mcrvo);
			}
		mcs.GeneratCardMakeTask(list);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("statusCode", "200");
		map.put("message", "任务生成成功");
		return map;
	}
	
	/**
	 * 设置对应卡产品的合作商
	 * @throws Exception 
	 */
	public  void setcompanyNo(MakeCardQueryResultVo mcrvo,String bankCode,String companyCode) throws Exception{
		if((pm.queryCardTypeCodeByName(mcrvo.getCardPName()).getCardTypeCode()).equals("1")){
			mcrvo.setCompanyNo(bankCode);
		}else{
			mcrvo.setCompanyNo(companyCode);
		}
		
	}
}
